package com.deyuanyun.pic.eqpt.service.impl;

import com.deyuanyun.pic.common.util.MapUtil;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.eqpt.domain.EmpUse;
import com.deyuanyun.pic.eqpt.domain.EquipmentInfo;
import com.deyuanyun.pic.eqpt.mapper.EmpUseMapper;
import com.deyuanyun.pic.eqpt.mapper.EquipmentInfoMapper;
import com.deyuanyun.pic.eqpt.service.EmpUseService;
import com.deyuanyun.pic.support.biz.CodeUtil;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.lang.reflect.InvocationTargetException;
import java.util.*;

/**
 * Created by Bill.li on 2015/12/30.
 */
@Service
public class EmpUseServiceImpl implements EmpUseService {
    @Resource
    private EmpUseMapper empUseMapper;
    @Resource
    private EquipmentInfoMapper equipmentInfoMapper;//添加借出时会清空设备的状态

    @Override
    public List<EmpUse> getInternally(HashMap<String,Object>  empUse) {
        return empUseMapper.getInternally(empUse);//获取内部使用页面数据和条件查询
    }

    @Override
    public Boolean addInternally(HashMap param) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        List<String> error=new ArrayList<String>();
        String id= UUID.randomUUID().toString().replaceAll("-", "");
        EmpUse info= (EmpUse) MapUtil.mapToObject(param, EmpUse.class);
        info.setId(id);
        if(StringUtils.isEmpty(info.getEqptEquipmentId())){
            error.add("设备不能为空!");
            param.put("errList", error);
            return false;
        }
        if(StringUtils.isEmpty(info.getUserId())){
            error.add("领用人不能为空!");
            param.put("errList", error);
            return false;
        }
        if(StringUtils.isEmpty(info.getStartDate().toString())){
            error.add("借出日期不能为空");
            param.put("errList", error);
            return false;
        }
        if(StringUtils.isEmpty(info.getEndDate().toString())){
            error.add("预计归还日期不能为空");
            param.put("errList", error);
            return false;
        }
        param.remove(id);//判断id是否存在
        empUseMapper.addInternally(info);
        
        //准备更新设备状态
        Map<String,String>  equipmentMap=new HashMap<String,String> ();
        equipmentMap.put("id", info.getEqptEquipmentId());
        equipmentMap.put("cstate", CodeUtil.getCodeId("EQPT_STATE", "服务中"));
        equipmentInfoMapper.updataLoan(equipmentMap);
        
//        info.setId("id");
//        EmpUse record=new EmpUse();
//        record.setUserId(info.getUserId());
//        record.setEqptEquipmentId(info.getEqptEquipmentId());
//        record.setStartDate(info.getStartDate());
//        record.setCreateUser(info.getCreateUser());
//        record.setId(info.getId());
//        if(record!=null){//当执行新增操作时更新设备信息表借出时间,预计归还时间的数据!
//            EquipmentInfo eqinfo=new EquipmentInfo();
//            eqinfo.setStartDate(info.getStartDate());
//            eqinfo.setPlanEndDate(info.getPlanEndDate());
//            equipmentInfoMapper.updateQuipment(eqinfo);
//        }
//        empUseMapper.updateInternally(record);
        return true;//更新设备信息
    }

    @Override
    public Boolean updateInternally(HashMap param) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        List<String> error=new ArrayList<String>();
        EmpUse info= (EmpUse) MapUtil.mapToObject(param, EmpUse.class);
        if(StringUtils.isEmpty(info.getEqptEquipmentId())){
            error.add("设备ID不能为空!");
            param.put("errList", error);
            return false;
        }
        if(StringUtils.isEmpty(info.getUserId())){
            error.add("领用人不能为空!");
            param.put("errList", error);
            return false;
        }
        if(StringUtils.isEmpty(info.getStartDate().toString())){
            error.add("借出日期不能为空");
            param.put("errList", error);
            return false;
        }
        if(StringUtils.isEmpty(info.getEndDate().toString())){
            error.add("预计归还日期不能为空");
            param.put("errList", error);
            return false;
        }
       
        
        
        //将原借出设备更新状态为闲置中 
        Map<String,String>  equipmentMap=new HashMap<String,String> ();
        equipmentMap.put("id", empUseMapper.findEquipment(info.getId()));
        equipmentMap.put("cstate", CodeUtil.getCodeId("EQPT_STATE", "闲置中"));
        equipmentInfoMapper.updataLoan(equipmentMap);
        
        empUseMapper.updateByPrimaryKeySelective(info);
        
        //将借出设备状态设置为服务中
        equipmentMap=new HashMap<String,String> ();
        equipmentMap.put("id", info.getEqptEquipmentId());
        equipmentMap.put("cstate", CodeUtil.getCodeId("EQPT_STATE", "服务中"));
        equipmentInfoMapper.updataLoan(equipmentMap);
        
        return true;//更新设备信息
    }

    @Override
    public Boolean delInternally(HashMap param) {
    	String id=(String) param.get("id");
    	if(!StringUtils.isEmpty(id))
    	{
    		//将原借出设备更新状态为闲置中 
            Map<String,String>  equipmentMap=new HashMap<String,String> ();
            equipmentMap.put("id", empUseMapper.findEquipment(id));
            equipmentMap.put("cstate", CodeUtil.getCodeId("EQPT_STATE", "闲置中"));
            equipmentInfoMapper.updataLoan(equipmentMap);
    	}
    	
        return empUseMapper.delInternally(param);//删除内部使用(多个删除)
    }
}
